<template>
  <div class="chat-layout" :class="{ 'dark-mode': isDarkMode }">
    <nav class="sidebar">
      <div class="sidebar-header">
        <img src="@/assets/logo.svg" alt="Logo" class="logo" />
        <h1 class="brand-name">AI Chat</h1>
      </div>
      
      <div class="conversation-list">
        <button class="new-chat" @click="startNewChat">
          <Icon icon="mdi:plus" />
          新对话
        </button>
        
        <div class="conversations">
          <div 
            v-for="chat in chatList" 
            :key="chat.id"
            class="conversation-item"
            :class="{ active: currentChatId === chat.id }"
            @click="selectChat(chat.id)"
          >
            <Icon icon="mdi:message-outline" />
            <span class="title">{{ chat.title }}</span>
            <button class="delete-btn" @click.stop="deleteChat(chat.id)">
              <Icon icon="mdi:delete-outline" />
            </button>
          </div>
        </div>
      </div>
      
      <div class="sidebar-footer">
        <div class="user-info" @click="showUserMenu = !showUserMenu">
          <div class="avatar">{{ userInitials }}</div>
          <span class="username">{{ authStore.user?.username }}</span>
          <Icon icon="mdi:chevron-down" />
          
          <div v-if="showUserMenu" class="user-menu">
            <button class="menu-item" @click="toggleDarkMode">
              <Icon :icon="isDarkMode ? 'mdi:white-balance-sunny' : 'mdi:moon-waning-crescent'" />
              {{ isDarkMode ? '浅色模式' : '深色模式' }}
            </button>
            <button class="menu-item" @click="handleLogout">
              <Icon icon="mdi:logout" />
              退出登录
            </button>
          </div>
        </div>
      </div>
    </nav>
    
    <main class="main-content">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </main>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { Icon } from '@iconify/vue'
import { useAuthStore } from '@/stores/auth'
import { useRouter } from 'vue-router'
import { useChatStore } from '@/stores/chat'

const router = useRouter()
const authStore = useAuthStore()
const isDarkMode = ref(false)
const showUserMenu = ref(false)
const chatStore = useChatStore()

const chatList = computed(() => chatStore.sessions)
const currentChatId = computed({
  get: () => chatStore.currentSessionId,
  set: (value) => chatStore.currentSessionId = value
})

const userInitials = computed(() => {
  const username = authStore.user?.username || ''
  return username.slice(0, 2).toUpperCase()
})

const toggleDarkMode = () => {
  isDarkMode.value = !isDarkMode.value
  document.documentElement.classList.toggle('dark', isDarkMode.value)
  showUserMenu.value = false
}

const handleLogout = () => {
  authStore.logout()
  router.push('/auth/login')
}

const startNewChat = () => {
  const newSession = chatStore.createSession()
  router.push(`/chat/${newSession.id}`)
}

const selectChat = async (chatId: string) => {
  try {
    currentChatId.value = chatId
    // 加载历史记录
    await chatStore.loadHistory(chatId)
    // 导航到对应的聊天页面
    router.push(`/chat/${chatId}`)
  } catch (error) {
    console.error('Failed to load chat history:', error)
    message.error('加载历史记录失败')
  }
}

const deleteChat = async (chatId: string) => {
  try {
    await chatStore.deleteSession(chatId)
  } catch (error) {
    console.error('Failed to delete chat:', error)
  }
}
</script>

<style lang="scss" scoped>
.chat-layout {
  display: flex;
  height: 100vh;
  background: var(--bg-color);
  color: var(--text-color);
  
  &.dark-mode {
    --bg-color: #1a1a1a;
    --text-color: #ffffff;
    --sidebar-bg: #2d2d2d;
    --hover-bg: rgba(255, 255, 255, 0.1);
  }
}

.sidebar {
  width: 260px;
  background: var(--sidebar-bg, #f5f5f5);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border-color, #e0e0e0);
  
  .sidebar-header {
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    
    .logo {
      width: 32px;
      height: 32px;
    }
    
    .brand-name {
      font-size: 20px;
      font-weight: 600;
    }
  }
}

.conversation-list {
  flex: 1;
  padding: 0 12px;
  overflow-y: auto;
  
  .new-chat {
    width: 100%;
    padding: 12px;
    background: var(--primary-color, #42b883);
    color: white;
    border: none;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    cursor: pointer;
    margin-bottom: 20px;
    
    &:hover {
      background: var(--primary-hover, #3aa876);
    }
  }
}

.conversation-item {
  padding: 12px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  margin-bottom: 4px;
  
  &:hover {
    background: var(--hover-bg, rgba(0, 0, 0, 0.05));
  }
  
  &.active {
    background: var(--hover-bg, rgba(0, 0, 0, 0.05));
  }
  
  .title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  .delete-btn {
    opacity: 0;
    background: none;
    border: none;
    color: inherit;
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
    
    &:hover {
      background: var(--hover-bg, rgba(0, 0, 0, 0.05));
    }
  }
  
  &:hover .delete-btn {
    opacity: 1;
  }
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border-color, #e0e0e0);
  
  .user-info {
    padding: 8px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    position: relative;
    
    &:hover {
      background: var(--hover-bg, rgba(0, 0, 0, 0.05));
    }
    
    .avatar {
      width: 32px;
      height: 32px;
      background: var(--primary-color, #42b883);
      color: white;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-weight: 500;
    }
    
    .username {
      flex: 1;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}

.user-menu {
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  background: var(--bg-color, white);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  padding: 8px;
  margin-bottom: 8px;
  
  .menu-item {
    width: 100%;
    padding: 8px;
    border: none;
    background: none;
    color: inherit;
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    border-radius: 4px;
    
    &:hover {
      background: var(--hover-bg, rgba(0, 0, 0, 0.05));
    }
  }
}

.main-content {
  flex: 1;
  overflow: hidden;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style> 